package com.zhongxi.guoyuan.adapter.coinShop;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.AttrItemBean;

import java.util.List;

/**
 * Created by Administrator on 2017/6/19.
 */

public class CoinItemAttrAdapter extends BaseAdapter {

    private List<AttrItemBean> attrs;
    private Context context;

    public CoinItemAttrAdapter(Context context, List<AttrItemBean> attrs) {
        this.context = context;
        this.attrs = attrs;
    }

    @Override
    public int getCount() {
        if (attrs != null && attrs.size() > 0) {

            return attrs.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return attrs.get(i).getName();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = View.inflate(context, R.layout.item_all_attr, null);
            vh.ItemText = (TextView) view.findViewById(R.id.ItemText);
            vh.layout = (LinearLayout) view.findViewById(R.id.layout);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
         final AttrItemBean attrItemBean = attrs.get(i);
        switch (attrItemBean.getStates()) {
            //选中
            case "0":
                vh.layout.setBackgroundResource(R.drawable.shape2);
                vh.ItemText.setTextColor(context.getResources().getColor(R.color.white));
                break;
            //未选中
            case "1":
                vh.layout.setBackgroundResource(R.drawable.shape1);
                vh.ItemText.setTextColor(context.getResources().getColor(R.color.bg_yield));
                break;
            //不可选
            case "2":
                vh.layout.setBackgroundResource(R.drawable.shape3);
                vh.ItemText.setTextColor(context.getResources().getColor(R.color.line_color_two));
                break;
        }
        vh.ItemText.setText(attrItemBean.getName());
        vh.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener!=null){
                    if (!"2".equals(attrItemBean.getStates())){
                        onItemClickListener.onItemClick(attrItemBean,i);
                    }
                }
            }
        });

        return view;
    }

    private class ViewHolder {
        private LinearLayout layout;
        private TextView ItemText;
    }
    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(AttrItemBean attrItemBean, int position);
    }
}
